﻿
namespace Patterns4Net.PatternEnforcer.Core
{
    using System;

    /// <summary>
    /// Provides a checker that delegates the checking to a lambda function.
    /// </summary>
    /// <typeparam name="TTarget"></typeparam>
    public class LambdaChecker<TTarget> : IChecker<TTarget> where TTarget : class
    {
        private readonly Func<TTarget, bool> predicate;

        private readonly Func<TTarget, string> error;

        public LambdaChecker(Func<TTarget, bool> predicate, Func<TTarget, string> error)
        {
            this.predicate = predicate;
            this.error = error;
        }

        public CheckerResult Check(TTarget subject)
        {
            return CheckerResult.CreateSimpleResult(
                this.predicate.Invoke(subject),
                new CheckerError(this.error.Invoke(subject)));
        }
    }
}
